<?php
/**
 * Created by PhpStorm.
 * User: 64
 * Date: 2017/3/23 0023
 * Time: 14:26
 */
namespace app\web\controller;
use app\src\seller\logic\SellerLogic;
use app\web\api\SellerApi;
use app\web\api\AlipayApi;
use app\src\mall\logic\MallLogic;
use app\src\user\logic\MemberConfigLogic;
use app\src\user\action\LoginAction;
class Shop extends  Weixin{


    protected function _initialize() {
        parent::_initialize();
        if (session("?memberinfo")) {
            $this->memberinfo = session("memberinfo");
        }else{
            if (session("?global_user")) {
                $this->userinfo = session("global_user");
                $openid=$this->userinfo['info']['openid'];
                $result = (new MemberConfigLogic())->getInfo(['wxopenid'=>$openid]);
                if($result['status']&&!empty($result['info'])){
                    $action = new LoginAction();
                    $uid=$result['info']['uid'];
                    $result = $action->loginByUID($uid,'device_id_um','pc','role_user');
                    if($result['status']) {
                        session("memberinfo", $result['info']);
                    }
                }else{
                    session_destroy();
                    $invite_code = $this->_param('invite_code');
                    $seller_id = $this->_param('seller_id');
                    $this->redirect('login/register_auto',['invite_code'=>$invite_code,'seller_id'=>$seller_id]);
                }
            }else{
                $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                //  dump($url);exit;
                $this->getWxuser($url);
                $openid=$this->userinfo['info']['openid'];
                //   dump($this->userinfo);
                $result = (new MemberConfigLogic())->getInfo(['wxopenid'=>$openid]);
                if($result['status']&&!empty($result['info'])){
                    $action = new LoginAction();
                    $uid=$result['info']['uid'];
                    $result = $action->loginByUID($uid,'device_id_um','pc','role_user');
                    if($result['status']) {
                        session("memberinfo", $result['info']);
                    }
                }else{
                    session_destroy();
                    $invite_code = $this->_param('invite_code');
                    $seller_id = $this->_param('seller_id');
                    $this->redirect('login/register_auto',['invite_code'=>$invite_code,'seller_id'=>$seller_id]);
                }
            }
//            $invite_code = $this->_param('invite_code');
//            $seller_id = $this->_param('seller_id');
//                $this->redirect('login/register_auto',['invite_code'=>$invite_code,'seller_id'=>$seller_id]);
            }

    }

    public function pay(){
        $this->assignTitle('付款');
        $seller_id=$this->_param('seller_id');
        $this->assign('seller_id', $seller_id);
        $uid=$this->memberinfo['uid'];
        $is_first = (new SellerLogic())->checkBuy($seller_id,$uid);
        if($is_first['status']){
            $this->assign('firstinfo',$is_first['info']);
        }
        $result = SellerApi::detail($seller_id);
        $this->assign('detail', $result['info']);
        return $this->fetch();
    }
    public function order(){
        $seller_id=$this->_param('seller_id');
        $seller_name=$this->_param('seller_name');
        $amount=$this->_param('amount');

        $amount=(float)$amount*100;
        $no_discount=$this->_param('no_discount');
        $no_discount=(float)$no_discount*100;
        $no_discount=(string)$no_discount;
        $note=$this->_param('note');
        $uid=$this->memberinfo['uid'];
        $result=AlipayApi::prePay($seller_id,$uid,$amount,$note,$no_discount);
        if ($result['status']) {
            $data =array(
                'order_code'=>$result['info']['order_code'],
                'price'=>$result['info']['fee'],
                'seller_name'=>$seller_name
            );
            $this->success('订单生成','', $data);
        } else {
            $this->error('订单生成失败', '', $result['info']);
        }

    }
    public function evaluate(){


        return $this->fetch();
    }

    public function index(){

       // $this->redirect('shop/evaluate');
        return $this->fetch();
    }

    public function shopdetail(){

        $this->assignTitle('商家详情');
        $SignPackage = $this->wxapi->getSignPackage();
        $this->assign('SignPackage', $SignPackage);
            $seller_id = $this->_param('seller_id');
            $result = SellerApi::detail($seller_id);
         //   $detail = json_decode($result['info']['seller']['detail'],true);
    //   dump($detail);exit;
        $main_img = explode(',',$result['info']['seller']['main_img']);
            $this->assign('main_img', $main_img);
            if(!empty($result['info']['seller']['detail']['img'])){
                $simg = $result['info']['seller']['detail']['img'];
                $s_img = explode(',',$simg);
                $this->assign('s_img', $s_img);
            }else{
                $result['info']['seller']['detail']['title'] ='';
                $result['info']['seller']['detail']['detail'] ='';
                $this->assign('s_img', []);
            }
            $this->assign('detail', $result['info']);


        return $this->fetch();
    }

    public function shoplist(){
        $this->assignTitle('附近商家');
        $cate = $this->_param('cate',0);
        $SignPackage = $this->wxapi->getSignPackage();
        $name = $this->_param('name','');
        $this->assign('name', $name);
        $this->assign('cate', $cate);
        $this->assign('SignPackage', $SignPackage);
        return $this->fetch();
    }

    public function shoplistdata(){


        $city_code = $this->_param('city_code');
        $lng = $this->_param('lng');
        $lat = $this->_param('lat');
        $name = $this->_param('name');
        $maxDistance = $this->_param('maxDistance');
        $page_index = $this->_param('p');
        $cate = $this->_param('cate');
        $page_size = 10;
            //$this->_param('page_size');
            $result = SellerApi::queryNearby($city_code, $lng, $lat, $name,$maxDistance,$page_index,$page_size,$cate);
            if ($result['status']) {
                $this->success('数据获取成功', '', $result['info']);
            } else {
                $this->error('数据获取失败', '', $result['info']);
            }

    }
    public function mall(){

        $this->assignTitle('商城');
        $result = (new MallLogic)->mallList();
        if($result['status']){
            $this->assign('malllist',$result['info']);
        }
        return $this->fetch();


    }

    public function shopCategory()
    {
        $this->assignTitle('联盟商家');
        $result = SellerApi::queryAllCategory();
        if($result['status']){
           $list =  [];
            $baseImgUrl = SITE_URL.'/static/web/img';
            foreach ($result['info'] as $val){

                if($val['id'] == 1){
                    $list[] = [
                        'caption' => $val['name'],
                        'cateId' => intval($val['id']),
                        'normal'  => $baseImgUrl . '/shopCategory/meishi_normal.png',
                        'hover'  => $baseImgUrl . '/shopCategory/meishi_hover.png',
                        'checked' => true,
                        'children' => $val['children']
                    ];
                }
                if($val['id'] == 2){
                    $list[] = [
                        'caption' => $val['name'],
                        'cateId' => intval($val['id']),
                        'normal'  => $baseImgUrl . '/shopCategory/yule_normal.png',
                        'hover'  => $baseImgUrl . '/shopCategory/yule_hover.png',
                        'checked' => false,
                        'children' => $val['children']
                    ];
                }
                if($val['id'] == 3){
                    $list[] = [
                        'caption' => $val['name'],
                        'cateId' => intval($val['id']),
                        'normal'  => $baseImgUrl . '/shopCategory/yifu_normal.png',
                        'hover'  => $baseImgUrl . '/shopCategory/yifu_hover.png',
                        'checked' => false,
                        'children' => $val['children']
                    ];
                }
                if($val['id'] == 4){
                    $list[] = [
                        'caption' => $val['name'],
                        'cateId' => intval($val['id']),
                        'normal'  => $baseImgUrl . '/shopCategory/lvxing_normal.png',
                        'hover'  => $baseImgUrl . '/shopCategory/lvxing_hover.png',
                        'checked' => false,
                        'children' => $val['children']
                    ];
                }

            }
            foreach ($list as &$val){
                $children = [];
                foreach ($val['children'] as $val2){
                    $children2 = [];
                    foreach ($val2['children'] as $val3){
                        $children2[] = [
                            'name' => $val3['name'],
                            'shop_number' => $val3['count'],
                            'cateId' => intval($val3['id']),
                            'icon' => getImgUrl($val3['icon'],120)
                        ];
                    }
                    $children[] = [
                        'name' => $val2['name'],
                        'shop_number' => $val2['count'],
                        'cateId' => intval($val2['id']),
                        'children' => $children2
                    ];
                }
                $val['children'] = $children;
            }

            $this->assign('category',json_encode($list, JSON_UNESCAPED_UNICODE));
        }
        return $this->fetch();

    }
}